﻿using System;
using System.Collections.Generic;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Address
{
   
    [Serializable]
    [VerfityAttribute(Id = "VerfityA0004", Name = "政令都市检证", CategoryType = EnumCategoryType.住所, Model = true)]
    public class VerfityA0004 : VerfityOneResult
    {
 

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (result == null)
            {
                return false;
            }

            if (entryCommon == "30" || entryCommon == "20")
            {
                return false;
            }

            foreach (List<string> rowResult in result)
            {

                foreach (string colReulst in rowResult)
                {
                  
                    bresult = CheckPrefectureAndCity(colReulst);

                    if (bresult)
                    {
                        break;
                    }
                }

                if (bresult)
                {
                    break;
                }

            }
            
           

            if (bresult)
            {
               Console.WriteLine("VerfityA0004");
            }

            return bresult;
        }

        /// <summary>
        /// 规则检证-政令都市
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool CheckPrefectureAndCity(string result)
        {
            bool bresult = false;

            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, result);

            string prefecture = splitResult.GetPrefecture();
            string city = splitResult.GetCity();
            string town = splitResult.GetTown();

            bresult = CheckPrefectureAndCity(prefecture, city, "1");
            //记载内容是政令都市并且xx区包含在[町]中时抽出(前提：市栏必须包含[市]字)
            if (bresult)
            {
                if (city.Contains("市") && town.Contains("区"))
                {
                    return true;
                }
            }
            else
            {
                bresult = CheckPrefectureAndCity(prefecture, city, "0");
                //记载内容不是政令都市并且xx区包含在[市]中时抽出(前提：市栏必须包含[市]字)
                if (bresult)
                {
                    if (city.Contains("市") && city.Contains("区"))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        private bool CheckPrefectureAndCity(string prefecture, string city, string shiteiToshi)
        {
            bool bresult = false;

            bresult = Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                 t => t.Type.Equals("p") &&
                 t.Name.Equals(prefecture) &&
                 t.ShiteiToshi.Equals(shiteiToshi)
                 );


            if (bresult)
            {
                bresult = Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                              t => t.Type.Equals("c") &&
                                   t.Name.Equals(city) &&
                                   t.ShiteiToshi.Equals(shiteiToshi)
                              );
            }

            return bresult;
        }
    }
}
